/*
CLEANING OF CAUSE OF DISPLACEMENT

Data 	: DS_Lev_AF_hist_varsel.dta
Folder 	: rawdata/SCB_Leverans_2016/Leverans 7 Jonas/DS_Lev_AF_hist_varsel.dta
Date	: 2017-05-10

Creator		: 	Jonas Cederlof	(JC)
Description :	
Notes		: 	


*/

********************************************************************************

clear
set more			 off
cap   log close 		_all

log using 	"../log/A1_clean_varelorsak.log", replace 
use 		"$rawdatapath/DS_Lev_AF_varselorsak.dta"

* Manual destring of variable
gen 	orsak = 4
replace orsak = 1 	if varselorsak=="ANNA"
replace orsak = 2 	if varselorsak=="EFFE"
replace orsak = 3 	if varselorsak=="FLVE"
replace orsak = 5 	if varselorsak=="KONK"
replace orsak = 6 	if varselorsak=="NEDL"

*Label values of new variable
lab define orsak 	1 "Anna" 2 "Effe" 3 "Flve" 4 "L*ge" 5 "Konk" 6 "Nedl"
lab val orsak orsak

*Control equality
tab orsak varselorsak

*Handle duplicates
duplicates report 	lopnr_varsellnr
duplicates tag 		lopnr_varsellnr, gen(dup_varselorsak)

*Let bankcrupty (KONK) and shutting down (NEDL) be priority reasons when duplicates
bys lopnr_varsellnr : egen maxorsak = max(orsak)
drop if dup>0 & orsak!=maxorsak & maxorsak==5 | dup>0 & orsak!=maxorsak & maxorsak==6

*No priority of dropping observations
duplicates drop lopnr_varsellnr, force
drop   maxorsak  varselorsak
rename orsak varselorsak

*Save
compress
save "$datapath/A1_varselorsak.dta", replace



use "$rawdatapath/varsel_cause_20152019.dta", clear

* Manual destring of variable
gen 	orsak = 4
replace orsak = 1 	if varselorsak=="ANNA"
replace orsak = 2 	if varselorsak=="EFFE"
replace orsak = 3 	if varselorsak=="FLVE"
replace orsak = 5 	if varselorsak=="KONK"
replace orsak = 6 	if varselorsak=="NEDL"

*Label values of new variable
lab define orsak 	1 "Anna" 2 "Effe" 3 "Flve" 4 "L*ge" 5 "Konk" 6 "Nedl"
lab val orsak orsak

*Control equality
tab orsak varselorsak

*Handle duplicates
duplicates report 	lopnr_varsellnr
duplicates tag 		lopnr_varsellnr, gen(dup_varselorsak)

*Let bankcrupty (KONK) and shutting down (NEDL) be priority reasons when duplicates
bys lopnr_varsellnr : egen maxorsak = max(orsak)
drop if dup>0 & orsak!=maxorsak & maxorsak==5 | dup>0 & orsak!=maxorsak & maxorsak==6

*No priority of dropping observations
duplicates drop lopnr_varsellnr, force
drop   maxorsak  varselorsak
rename orsak varselorsak

save "$datapath/A1_varselorsak_16_19.dta", replace

log close


